package com.mtl.front.controller;

import com.mtl.common.tool.model.Page;
import com.mtl.discuss.domain.Discuss;
import com.mtl.discuss.service.IDiscussService;
import com.mtl.front.domain.Article;
import com.mtl.front.domain.Member;
import com.mtl.front.domain.Sort;
import com.mtl.front.service.IArticleService;
import com.mtl.front.service.IMemberService;
import com.mtl.front.service.ISortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.*;

@Controller
@RequestMapping("article")
public class ThymeleafMemberController {
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ISortService sortService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IDiscussService discussService;

    /**
     * 首页
     * @param model
     * @return
     */
  /*  @RequestMapping("/index")
    public String goIndex(Model model) {
        return "index";
    }*/

    /**
     * 分页显示已分类的文章
     * @return
     */
    @RequestMapping("articlePage")
    public String goSortPage(@RequestParam Map<String, Object> param, Model model) {
        Integer sortId = Integer.valueOf((String) param.get("sortId"));
        Integer size= Integer.valueOf((String) param.get("size"));
        Integer frontPage= Integer.valueOf((String) param.get("page"));
        model.addAttribute("frontPage",frontPage);
        Sort sort = sortService.findById(sortId);
        model.addAttribute("sortName", sort.getName());
        model.addAttribute("sortId",sortId);
        //对已分类的文章进行分页
        Page<Article> page = articleService.selectArticlePage(param);
        double total =page.getTotal();
        int pages = (int) Math.ceil(total/size);
        model.addAttribute("pages",pages);
        List<Article> articleList = page.getList();
        model.addAttribute("articleList", articleList);
        return "articlePage";
    }


    /**
     * 文章分类
     * @return
     */
    @RequestMapping("articleSort")
    public String goArticleSort(Model model) {
        model.addAttribute("sort1", new Sort());
        List<Sort> sorts = sortService.findAll();

        /**
         * 将文章分类写成树型
         */
        List<Map<String, Object>> trees = new ArrayList<>();
        for (Sort sort : sorts) {
            Map<String, Object> tree = new HashMap<>();
            if (sort.getPid() == -1) {
                tree.put("sortId", sort.getSortId());
                tree.put("name", sort.getName());
                List<Sort> child = new ArrayList<>();
                for (Sort s : sorts) {
                    if (s.getPid() == sort.getSortId())
                        child.add(s);
                }
                tree.put("children", child);
                trees.add(tree);
            }
        }
        model.addAttribute("sort", trees);

        /**
         * 将文章大类及其下面的文章写成树型
         */
        List<Article> articles = articleService.orderArticleByPostTime();
        List<Map<String, Object>> articleParentSort = new ArrayList<>();
        for (Sort sort1 : sorts) {
            Map<String, Object> tree = new HashMap<>();
            if (sort1.getPid() == -1) {
                tree.put("sortId", sort1.getSortId());
                tree.put("sortName", sort1.getName());
                articleParentSort.add(tree);
            }
        }
        /*System.out.println(articleParentSort);*/
        List<Map<String,Object>> sortedArticle=new ArrayList<>();
        for (Map<String, Object> map : articleParentSort
        ) {
            Map<String,Object> map1=new HashMap<>();
            map1.put("sortId",map.get("sortId"));
            map1.put("sortName",map.get("sortName"));
            List<Article> articleList=new ArrayList<>();
            for (Article a:articles
                 ) {
                if (a.getSortId() == map.get("sortId")) {
                    /*System.out.println(a.getTitle());*/
                    articleList.add(a);
                }
            }
            map1.put("sortedArticleList",articleList);
            sortedArticle.add(map1);
        }
        /*System.out.println(sortedArticle);*/
        model.addAttribute("sortedArticle",sortedArticle);

        Page<Member> detail=memberService.findPage(2,5);
        System.out.println(detail);
        model.addAttribute("detail",detail);
        return "articleSort";
    }

    /**
     * 登录注册
     * @return
     */
   /* @RequestMapping("contact")
    public String goContact() {

        return "contact";
    }*/

    /**
     * 关于我们
     *
     * @return
     */
    @RequestMapping("about")
    public String goAbout() {

        return "about";
    }

    /**
     * 文章详情
     *
     * @return
     */
    @RequestMapping("detail")
    public String goDetail(Model model, @RequestParam Integer id) {
        System.out.println(id);
        model.addAttribute("article", articleService.findById(id));
        List<Discuss> discusses = discussService.selectByArticleId(id);
        model.addAttribute("discusses",discusses);
        return "detail";
    }

    @RequestMapping("addDis")
    public String discuss(Model model, @RequestParam String dis, Integer artid, HttpServletRequest rq) {
        System.out.println(dis);
        System.out.println(rq.getSession().getId());
        System.out.println(artid);
        Discuss dis1=new Discuss();
        dis1.setArtId(artid);
        dis1.setDiscuss(dis);
        discussService.saveOrUpdate(dis1);
        model.addAttribute("article", articleService.findById(artid));
        return "detail";
    }

}
